home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / ui / scrollprintsettings.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  35.7 KB  |  948 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2007 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20. #
  21.  
  22. # Local
  23. from base.g import *
  24. from base import utils
  25. from prnt import cups
  26.  
  27. # Qt
  28. from qt import *
  29. from scrollview import ScrollView
  30.  
  31. # Std Lib
  32. import os.path, os
  33.  
  34.  
  35. class OptionComboBox(QComboBox):
  36.     def __init__(self, rw, parent, name, group, option, choices, default, typ=cups.PPD_UI_PICKONE, other=None):
  37.         QComboBox.__init__(self, rw, parent, name)
  38.         self.group = group
  39.         self.option = option
  40.         self.choices = choices
  41.         self.default = default
  42.         self.typ = typ
  43.         self.other = other
  44.  
  45.     def setDefaultPushbutton(self, pushbutton):
  46.         self.pushbutton = pushbutton
  47.         
  48.     def setOther(self, other):
  49.         self.other = other
  50.  
  51.  
  52. class OptionSpinBox(QSpinBox):
  53.     def __init__(self,  parent, name, group, option, default):
  54.         QSpinBox.__init__(self, parent, name)
  55.         self.group = group
  56.         self.option = option
  57.         self.default = default
  58.  
  59.     def setDefaultPushbutton(self, pushbutton):
  60.         self.pushbutton = pushbutton
  61.  
  62.  
  63. class OptionButtonGroup(QButtonGroup):
  64.     def __init__(self,  parent, name, group, option, default):
  65.         QButtonGroup.__init__(self, parent, name)
  66.         self.group = group
  67.         self.option = option
  68.         self.default = default
  69.  
  70.     def setDefaultPushbutton(self, pushbutton):
  71.         self.pushbutton = pushbutton
  72.             
  73.         
  74. class DefaultPushButton(QPushButton):
  75.     def __init__(self,  parent, name, group, option, choices, default, control, typ):
  76.         QPushButton.__init__(self, parent, name)
  77.         self.group = group
  78.         self.option = option
  79.         self.default = default
  80.         self.control = control
  81.         self.typ = typ
  82.         self.choices = choices
  83.         
  84.  
  85.  
  86. class ScrollPrintSettingsView(ScrollView):
  87.     def __init__(self,parent = None,name = None,fl = 0):
  88.         ScrollView.__init__(self,parent,name,fl)
  89.  
  90.     def fillControls(self):
  91.         ScrollView.fillControls(self)
  92.         
  93.         self.loading = True
  94.         cups.resetOptions()
  95.         cups.openPPD(self.cur_printer)
  96.  
  97.         try:
  98.             try:
  99.                 current_options = dict(cups.getOptions())
  100.                 
  101.                 if not self.is_fax:
  102.                     self.addGroupHeading("basic", self.__tr("Basic"))
  103.                     log.debug("Group: Basic")
  104.     
  105.                     # Basic
  106.                         # PageSize (in PPD section)
  107.                         # orientation-requested
  108.                         # sides
  109.                         # outputorder
  110.                         # Collate
  111.     
  112.                     
  113.                     current = current_options.get('orientation-requested', '3')
  114.     
  115.                     self.addItem("basic", "orientation-requested", self.__tr("Page Orientation"), 
  116.                         cups.PPD_UI_PICKONE, current, 
  117.                         [('3', self.__tr('Portrait')), 
  118.                          ('4', self.__tr('Landscape')), 
  119.                          ('5', self.__tr('Reverse landscape')), 
  120.                          ('6', self.__tr('Reverse portrait'))], '3')
  121.     
  122.                     log.debug("Option: orientation-requested")
  123.                     log.debug("Current value: %s" % current)
  124.     
  125.                     duplexer = self.cur_device.dq.get('duplexer', 0)
  126.                     log.debug("Duplexer = %d" % duplexer)
  127.     
  128.                     if duplexer:
  129.                         current = current_options.get('sides', 'one-sided')
  130.                         self.addItem("basic", "sides", 
  131.                             self.__tr("Duplex (Print on both sides of the page)"), 
  132.                             cups.PPD_UI_PICKONE, current, 
  133.                             [('one-sided',self.__tr('Single sided')), 
  134.                              ('two-sided-long-edge', self.__tr('Two sided (long edge)')), 
  135.                              ('two-sided-short-edge', self.__tr('Two sided (short edge)'))], 'one-sided')
  136.     
  137.                         log.debug("Option: sides")
  138.                         log.debug("Current value: %s" % current)
  139.     
  140.                     current = current_options.get('outputorder', 'normal')
  141.     
  142.                     self.addItem("basic", "outputorder", 
  143.                         self.__tr("Output Order (Print last page first)"), 
  144.                         cups.PPD_UI_PICKONE, current, 
  145.                         [('normal', self.__tr('Normal (Print first page first)')), 
  146.                          ('reverse', self.__tr('Reversed (Print last page first)'))], 'normal')
  147.     
  148.                     log.debug("Option: outputorder")
  149.                     log.debug("Current value: %s" % current)
  150.     
  151.                     current = int(utils.to_bool(current_options.get('Collate', '0')))
  152.     
  153.                     self.addItem("basic", "Collate", 
  154.                         self.__tr("Collate (Group together multiple copies)"), 
  155.                         cups.PPD_UI_BOOLEAN, current, 
  156.                         [], 0)
  157.     
  158.                     log.debug("Option: Collate")
  159.                     log.debug("Current value: %s" % current)
  160.  
  161.                 groups = cups.getGroupList()
  162.  
  163.                 for g in groups:
  164.                     log.debug("Group: %s" % g)
  165.                     text, num_subgroups = cups.getGroup(g) 
  166.                     read_only = 'install' in g.lower()
  167.                     
  168.                     if g.lower() == 'printoutmode':
  169.                         text = self.__tr("Quality")
  170.                     
  171.                     self.addGroupHeading(g, text, read_only)
  172.  
  173.                     log.debug("  Text: %s" % text)
  174.                     log.debug("Num subgroups: %d" % num_subgroups)
  175.  
  176.                     options = cups.getOptionList(g)
  177.  
  178.                     for o in options:
  179.                         log.debug("  Option: %s" % o)
  180.  
  181.                         if 'pageregion' in o.lower():
  182.                             log.debug("Skipped.")
  183.                             continue
  184.  
  185.                         option_text, defchoice, conflicted, ui  = cups.getOption(g, o)
  186.  
  187.                         if o.lower() == 'quality':
  188.                             option_text = self.__tr("Quality")
  189.                         
  190.                         log.debug("    Text: %s" % option_text)
  191.                         log.debug("    Defchoice: %s" % defchoice)
  192.  
  193.                         choices = cups.getChoiceList(g, o)
  194.  
  195.                         value = None
  196.                         choice_data = []
  197.                         for c in choices:
  198.                             log.debug("    Choice: %s" % c)
  199.  
  200.                             # TODO: Add custom paper size controls
  201.                             if 'pagesize' in o.lower() and 'custom' in c.lower():
  202.                                 log.debug("Skipped.")
  203.                                 continue
  204.  
  205.                             choice_text, marked = cups.getChoice(g, o, c)
  206.  
  207.                             choice_text = choice_text.decode('utf-8')
  208.                             
  209.                             log.debug("      Text: %s" % choice_text)
  210.  
  211.                             if marked:
  212.                                 value = c
  213.  
  214.                             choice_data.append((c, choice_text))
  215.  
  216.  
  217.                         self.addItem(g, o, option_text, ui, value, choice_data, defchoice, read_only)
  218.                         
  219. ##                        if 'pagesize' in o.lower(): # and 'custom' in c.lower():
  220. ##                            current = 0.0
  221. ##                            width_widget = self.addItem("custom", "custom-width", self.__tr("Custom Paper Width"), cups.UI_UNITS_SPINNER,
  222. ##                                current, (0.0, 0.0), 0.0) 
  223. ##                            
  224. ##                            current = 0.0
  225. ##                            height_widget = self.addItem("custom", "custom-height", self.__tr("Custom Paper Height"), cups.UI_UNITS_SPINNER,
  226. ##                                current, (0.0, 0.0), 0.0) 
  227. ##                                
  228. ##                            if value.lower() == 'custom':
  229. ##                                pass
  230.  
  231.                 # N-Up
  232.                     # number-up
  233.                     # number-up-layout
  234.                     # page-border
  235.  
  236.                 self.addGroupHeading("nup", 
  237.                     self.__tr("N-Up (Multiple document pages per printed page)"))
  238.  
  239.                 log.debug("Group: N-Up")
  240.  
  241.                 current = current_options.get('number-up', '1')
  242.  
  243.                 self.addItem("nup", "number-up", self.__tr("Pages per Sheet"), 
  244.                     cups.PPD_UI_PICKONE, current, 
  245.                     [('1', self.__tr('1 sheet per page')), 
  246.                      ('2', self.__tr('2 sheets per page')), 
  247.                      ('4', self.__tr('4 sheets per page'))], '1')
  248.  
  249.                 log.debug("  Option: number-up")
  250.                 log.debug("  Current value: %s" % current)
  251.  
  252.                 current = current_options.get('number-up-layout', 'lrtb')
  253.  
  254.                 self.addItem("nup", "number-up-layout", self.__tr("Layout"), 
  255.                     cups.PPD_UI_PICKONE, current, 
  256.                     [('btlr', self.__tr('Bottom to top, left to right')), 
  257.                      ('btrl', self.__tr('Bottom to top, right to left')), 
  258.                      ('lrbt', self.__tr('Left to right, bottom to top')), 
  259.                      ('lrtb', self.__tr('Left to right, top to bottom')),
  260.                      ('rlbt', self.__tr('Right to left, bottom to top')), 
  261.                      ('rltb', self.__tr('Right to left, top to bottom')),
  262.                      ('tblr', self.__tr('Top to bottom, left to right')), 
  263.                      ('tbrl', self.__tr('Top to bottom, right to left')) ], 'lrtb')
  264.  
  265.                 log.debug("  Option: number-up-layout")
  266.                 log.debug("  Current value: %s" % current)
  267.  
  268.                 current = current_options.get('page-border', 'none')
  269.  
  270.                 self.addItem("nup", "page-border", 
  271.                     self.__tr("Printed Border Around Each Page"), 
  272.                     cups.PPD_UI_PICKONE, current,
  273.                     [('double', self.__tr("Two thin borders")), 
  274.                      ("double-thick", self.__tr("Two thick borders")),
  275.                      ("none", self.__tr("No border")), 
  276.                      ("single", self.__tr("One thin border")), 
  277.                      ("single-thick", self.__tr("One thick border"))], 'none')
  278.  
  279.                 log.debug("  Option: page-border")
  280.                 log.debug("  Current value: %s" % current)
  281.  
  282.                 # Adjustment
  283.                     # brightness
  284.                     # gamma
  285.  
  286.                 if not self.is_fax:
  287.                     self.addGroupHeading("adjustment", self.__tr("Printout Appearance"))
  288.     
  289.                     current = int(current_options.get('brightness', 100))
  290.     
  291.                     log.debug("  Option: brightness")
  292.                     log.debug("  Current value: %s" % current)
  293.     
  294.                     self.addItem("adjustment", "brightness", self.__tr("Brightness"),
  295.                         cups.UI_SPINNER, current, (0, 200), 100, suffix=" %")
  296.     
  297.                     current = int(current_options.get('gamma', 1000))
  298.     
  299.                     log.debug("  Option: gamma")
  300.                     log.debug("  Current value: %s" % current)
  301.     
  302.                     self.addItem("adjustment", "gamma", self.__tr("Gamma"), cups.UI_SPINNER, current,
  303.                         (1, 10000), 1000)
  304.     
  305.                 # Margins (pts)
  306.                     # page-left
  307.                     # page-right
  308.                     # page-top
  309.                     # page-bottom
  310.  
  311. ##                if 0:
  312. ##                    # TODO: cupsPPDPageSize() fails on LaserJets. How do we get margins in this case? Defaults?
  313. ##                    # PPD file for LJs has a HWMargin entry...
  314. ##                    page, page_width, page_len, left, bottom, right, top = cups.getPPDPageSize()
  315. ##
  316. ##                    right = page_width - right
  317. ##                    top = page_len - top
  318. ##
  319. ##                    self.addGroupHeading("margins", self.__tr("Margins"))
  320. ##                    current_top = current_options.get('page-top', 0) # pts
  321. ##                    current_bottom = current_options.get('page-bottom', 0) # pts
  322. ##                    current_left = current_options.get('page-left', 0) # pts
  323. ##                    current_right = current_options.get('page-right', 0) # pts
  324. ##
  325. ##                    log.debug("  Option: page-top")
  326. ##                    log.debug("  Current value: %s" % current_top)
  327. ##
  328. ##                    self.addItem("margins", "page-top", self.__tr("Top margin"), 
  329. ##                        cups.UI_UNITS_SPINNER, current_top,
  330. ##                        (0, page_len), top)
  331. ##
  332. ##                    self.addItem("margins", "page-bottom", self.__tr("Bottom margin"), 
  333. ##                        cups.UI_UNITS_SPINNER, current_bottom,
  334. ##                        (0, page_len), bottom)
  335. ##
  336. ##                    self.addItem("margins", "page-left", self.__tr("Right margin"), 
  337. ##                        cups.UI_UNITS_SPINNER, current_left,
  338. ##                        (0, page_width), left)
  339. ##
  340. ##                    self.addItem("margins", "page-right", self.__tr("Left margin"), 
  341. ##                        cups.UI_UNITS_SPINNER, current_right,
  342. ##                        (0, page_width), right)
  343.  
  344.                 # Image Printing
  345.                     # position
  346.                     # natural-scaling
  347.                     # saturation
  348.                     # hue
  349.  
  350.                 self.addGroupHeading("image", self.__tr("Image Printing"))
  351.  
  352.                 current = current_options.get('fitplot', 'false')
  353.                 
  354.                 self.addItem("image", "fitplot", 
  355.                     self.__tr("Fit to page"), 
  356.                     cups.PPD_UI_BOOLEAN, current, 
  357.                     [], 0)
  358.  
  359.                 
  360.                 current = current_options.get('position', 'center')
  361.  
  362.                 self.addItem("image", "position", self.__tr("Position on Page"), 
  363.                     cups.PPD_UI_PICKONE, current, 
  364.                     [('center', self.__tr('Centered')), 
  365.                      ('top', self.__tr('Top')), 
  366.                      ('left', self.__tr('Left')), 
  367.                      ('right', self.__tr('Right')), 
  368.                      ('top-left', self.__tr('Top left')), 
  369.                      ('top-right', self.__tr('Top right')), 
  370.                      ('bottom', self.__tr('Bottom')),
  371.                      ('bottom-left', self.__tr('Bottom left')), 
  372.                      ('bottom-right', self.__tr('Bottom right'))], 'center')
  373.  
  374.                 log.debug("  Option: position")
  375.                 log.debug("  Current value: %s" % current)
  376.  
  377.                 if not self.is_fax:
  378.                     current = int(current_options.get('saturation', 100))
  379.     
  380.                     log.debug("  Option: saturation")
  381.                     log.debug("  Current value: %s" % current)
  382.     
  383.                     self.addItem("image", "saturation", self.__tr("Saturation"), 
  384.                         cups.UI_SPINNER, current, (0, 200), 100, suffix=" %")
  385.     
  386.                     current = int(current_options.get('hue', 0))
  387.     
  388.                     log.debug("  Option: hue")
  389.                     log.debug("  Current value: %s" % current)
  390.     
  391.                     self.addItem("image", "hue", self.__tr("Hue (color shift/rotation)"), 
  392.                         cups.UI_SPINNER, current,
  393.                         (-100, 100), 0)
  394.  
  395.                 current = int(current_options.get('natural-scaling', 100))
  396.  
  397.                 log.debug("  Option: natural-scaling")
  398.                 log.debug("  Current value: %s" % current)
  399.  
  400.                 self.addItem("image", "natural-scaling", 
  401.                     self.__tr('"Natural" Scaling (relative to image)'), 
  402.                     cups.UI_SPINNER, current, (1, 800), 100, suffix=" %")
  403.  
  404.                 current = int(current_options.get('scaling', 100))
  405.  
  406.                 log.debug("  Option: scaling")
  407.                 log.debug("  Current value: %s" % current)
  408.  
  409.                 self.addItem("image", "scaling", self.__tr("Scaling (relative to page)"), 
  410.                     cups.UI_SPINNER, current,
  411.                     (1, 800), 100, suffix=" %")
  412.  
  413.                 # Misc
  414.                     # PrettyPrint
  415.                     # job-sheets
  416.                     # mirror
  417.  
  418.                 self.addGroupHeading("misc", self.__tr("Miscellaneous"))
  419.  
  420.                 log.debug("Group: Misc")
  421.  
  422.                 current = int(utils.to_bool(current_options.get('prettyprint', '0')))
  423.  
  424.                 self.addItem("misc", "prettyprint", 
  425.                     self.__tr('"Pretty Print" Text Documents (Add headers and formatting)'),
  426.                     cups.PPD_UI_BOOLEAN, current, [], 0)
  427.  
  428.                 log.debug("  Option: prettyprint")
  429.                 log.debug("  Current value: %s" % current)
  430.  
  431.                 if not self.is_fax:
  432.                     current = current_options.get('job-sheets', 'none').split(',')
  433.                     
  434.                     try:
  435.                         start = current[0]
  436.                     except IndexError:
  437.                         start = 'none'
  438.                         
  439.                     try:
  440.                         end = current[1]
  441.                     except IndexError:
  442.                         end = 'none'
  443.                     
  444.                     # TODO: Look for locally installed banner pages beyond the default CUPS ones?
  445.                     self.addItem("misc", "job-sheets", self.__tr("Banner Pages"), cups.UI_BANNER_JOB_SHEETS, 
  446.                         (start, end), 
  447.                         [("none", self.__tr("No banner page")), 
  448.                          ('classified', self.__tr("Classified")), 
  449.                          ('confidential', self.__tr("Confidential")),
  450.                          ('secret', self.__tr("Secret")), 
  451.                          ('standard', self.__tr("Standard")), 
  452.                          ('topsecret', self.__tr("Top secret")), 
  453.                          ('unclassified', self.__tr("Unclassified"))], ('none', 'none'))
  454.                     
  455.                     log.debug("  Option: job-sheets")
  456.                     log.debug("  Current value: %s,%s" % (start, end))
  457.  
  458.                 current = int(utils.to_bool(current_options.get('mirror', '0')))
  459.  
  460.                 self.addItem("misc", "mirror", self.__tr('Mirror Printing'),
  461.                     cups.PPD_UI_BOOLEAN, current, [], 0)
  462.  
  463.                 log.debug("  Option: mirror")
  464.                 log.debug("  Current value: %s" % current)
  465.  
  466.             except Exception, e:
  467.                 log.exception()
  468.  
  469.         finally:
  470.             cups.closePPD()
  471.             self.loading = False
  472.  
  473.     def optionComboBox_activated(self, a):
  474.         a = str(a)
  475.         sender = self.sender()
  476.         choice = None
  477.         
  478.         if sender.typ == cups.UI_BANNER_JOB_SHEETS:
  479.             start, end = None, None
  480.             for c, t in sender.choices:
  481.                 if t == a:
  482.                     start = c
  483.                     break
  484.                     
  485.             for c, t in sender.other.choices:
  486.                 if t == sender.other.currentText():
  487.                     end = c
  488.                     break
  489.                 
  490.             if sender.option == 'end':
  491.                 start, end = end, start
  492.  
  493.             if start is not None and \
  494.                 end is not None and \
  495.                 start.lower() == sender.default[0].lower() and \
  496.                 end.lower() == sender.default[1].lower():
  497.                     self.removePrinterOption('job-sheets')
  498.                     sender.pushbutton.setEnabled(False)
  499.             else:
  500.                 sender.pushbutton.setEnabled(True)
  501.                 
  502.                 if start is not None and \
  503.                     end is not None:
  504.                     
  505.                     self.setPrinterOption('job-sheets', ','.join([start, end]))
  506.                 
  507.         else:
  508.             choice = None
  509.             for c, t in sender.choices:
  510.                 if t == a:
  511.                     choice = c
  512.                     break
  513.     
  514.             if choice is not None and choice.lower() == sender.default.lower():
  515.                 self.removePrinterOption(sender.option)
  516.                 sender.pushbutton.setEnabled(False)
  517.             else:
  518.                 sender.pushbutton.setEnabled(True)
  519.     
  520.                 if choice is not None:
  521.                     self.setPrinterOption(sender.option, choice)
  522.                     
  523.             self.linkPrintoutModeAndQuality(sender.option, choice)
  524.  
  525.     
  526.     def linkPrintoutModeAndQuality(self, option, choice):
  527.         if option.lower() == 'quality' and \
  528.             choice is not None:
  529.             
  530.             try:
  531.                 c = self.items['o:PrintoutMode'].control
  532.             except KeyError:
  533.                 return
  534.             else:
  535.                 if c is not None:
  536.                     if choice.lower() == 'fromprintoutmode':
  537.                         # from printoutmode selected
  538.                         # determine printoutmode option combo enable state
  539.                         c.setEnabled(True)
  540.                         QToolTip.remove(c)
  541.                         a = str(c.currentText())
  542.                         
  543.                         # determine printoutmode default button state
  544.                         link_choice = None
  545.                         for x, t in c.choices:
  546.                             if t == a:
  547.                                 link_choice = x
  548.                                 break
  549.                 
  550.                         if link_choice is not None and \
  551.                             link_choice.lower() == c.default.lower():
  552.                             
  553.                             c.pushbutton.setEnabled(False)
  554.                         else:
  555.                             c.pushbutton.setEnabled(True)
  556.                     
  557.                     else: # fromprintoutmode not selected, disable printoutmode
  558.                         c.setEnabled(False)
  559.                         QToolTip.add(c, self.__tr("""Set Quality to "Controlled by 'Printout Mode'" to enable."""))
  560.                         c.pushbutton.setEnabled(False)
  561.                             
  562.  
  563.  
  564.     def optionSpinBox_valueChanged(self, i):
  565.         sender = self.sender()
  566.  
  567.         if i == sender.default:
  568.             self.removePrinterOption(sender.option)
  569.             sender.pushbutton.setEnabled(False)
  570.         else:
  571.             sender.pushbutton.setEnabled(True)
  572.             self.setPrinterOption(sender.option, str(i))
  573.  
  574.             
  575.     def optionButtonGroup_clicked(self, b):
  576.         sender = self.sender()
  577.         b = int(b)
  578.  
  579.         if b == sender.default:
  580.             self.removePrinterOption(sender.option)
  581.             sender.pushbutton.setEnabled(False)
  582.         else:
  583.             sender.pushbutton.setEnabled(True)
  584.  
  585.             if b:
  586.                 self.setPrinterOption(sender.option, "true")
  587.             else:
  588.                 self.setPrinterOption(sender.option, "false")
  589.         
  590.  
  591.  
  592.     def defaultPushButton_clicked(self):
  593.         sender = self.sender()
  594.         sender.setEnabled(False)
  595.  
  596.         if sender.typ == cups.PPD_UI_BOOLEAN:
  597.             if sender.default:
  598.                 sender.control.setButton(1)
  599.             else:
  600.                 sender.control.setButton(0)
  601.                 
  602.             self.removePrinterOption(sender.option)
  603.  
  604.         elif sender.typ == cups.PPD_UI_PICKONE:
  605.             choice, text = None, None
  606.  
  607.             for c, t in sender.choices:
  608.                 if c == sender.default:
  609.                     choice = c
  610.                     text = t
  611.                     break
  612.  
  613.             if choice is not None:
  614.                 self.removePrinterOption(sender.option)
  615.                 sender.control.setCurrentText(text)
  616.                 
  617.                 self.linkPrintoutModeAndQuality(sender.option, choice)
  618.  
  619.         elif sender.typ == cups.UI_SPINNER:
  620.             sender.control.setValue(sender.default)
  621.             self.removePrinterOption(sender.option)
  622.             
  623.         elif sender.typ == cups.UI_BANNER_JOB_SHEETS:
  624.             start, end, start_text, end_text = None, None, None, None
  625.             for c, t in sender.choices:
  626.                 if c == sender.default[0]:
  627.                     start = c
  628.                     start_text = t
  629.                 
  630.                 if c == sender.default[1]:
  631.                     end = c
  632.                     end_text = t
  633.                     
  634.             if start is not None:
  635.                 sender.control[0].setCurrentText(start_text)
  636.                 
  637.             if end is not None:
  638.                 sender.control[1].setCurrentText(end_text)
  639.                 
  640.             self.removePrinterOption('job-sheets')
  641.                 
  642.  
  643.     def setPrinterOption(self, option, value):
  644.         cups.openPPD(self.cur_printer)
  645.  
  646.         try:
  647.             cups.addOption("%s=%s" % (option, value))
  648.             cups.setOptions()
  649.         finally:
  650.             cups.closePPD()
  651.  
  652.     def removePrinterOption(self, option):
  653.         cups.openPPD(self.cur_printer)
  654.  
  655.         try:
  656.             cups.removeOption(option)
  657.             cups.setOptions()
  658.         finally:
  659.             cups.closePPD()
  660.  
  661.  
  662.     def addItem(self, group, option, text, typ, value, choices, default, read_only=False, suffix=""):
  663.         widget, control = None, None
  664.  
  665.         if typ == cups.PPD_UI_BOOLEAN: # () On (*) Off widget
  666.             widget = self.getWidget()
  667.             layout = QGridLayout(widget, 1, 1, 5, 10, "layout")
  668.             default = int(utils.to_bool(str(default)))
  669.             value = int(utils.to_bool(str(value)))
  670.     
  671.             textLabel1 = QLabel(widget, "textLabel1")
  672.             layout.addWidget(textLabel1, 0, 0)
  673.     
  674.             buttonGroup = OptionButtonGroup(widget, "buttonGroup", group, option, default)
  675.             buttonGroup.setLineWidth(0)
  676.             buttonGroup.setColumnLayout(0,Qt.Vertical)
  677.             buttonGroup.layout().setSpacing(1)
  678.             buttonGroup.layout().setMargin(5)
  679.             buttonGroupLayout = QHBoxLayout(buttonGroup.layout())
  680.             buttonGroupLayout.setAlignment(Qt.AlignTop)
  681.     
  682.             defaultPushButton = DefaultPushButton(widget,"defaultPushButton", group, option, 
  683.                 choices, default, buttonGroup, typ)    
  684.     
  685.             buttonGroup.setDefaultPushbutton(defaultPushButton)
  686.             
  687.             layout.addWidget(defaultPushButton, 0, 3)
  688.             
  689.             spacer1 = QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
  690.             layout.addItem(spacer1, 0, 1)
  691.             
  692.             onRadioButton = QRadioButton(buttonGroup,"onRadioButton")
  693.             buttonGroup.insert(onRadioButton, 1)
  694.             buttonGroupLayout.addWidget(onRadioButton)
  695.     
  696.             offRadioButton = QRadioButton(buttonGroup,"offRadioButton")
  697.             buttonGroup.insert(offRadioButton, 0)
  698.             buttonGroupLayout.addWidget(offRadioButton)
  699.     
  700.             layout.addWidget(buttonGroup, 0, 2)
  701.             
  702.             textLabel1.setText(text)
  703.             onRadioButton.setText(self.__tr("On"))
  704.             offRadioButton.setText(self.__tr("Off"))
  705.             
  706.             if value == default:
  707.                 defaultPushButton.setEnabled(False)
  708.                 
  709.             self.connect(defaultPushButton, SIGNAL("clicked()"), self.defaultPushButton_clicked)
  710.             self.connect(buttonGroup, SIGNAL("clicked(int)"), self.optionButtonGroup_clicked)
  711.  
  712.             x = self.__tr('Off')
  713.             if default:
  714.                 x = self.__tr('On')
  715.  
  716.             if value:
  717.                 buttonGroup.setButton(1)
  718.             else:
  719.                 buttonGroup.setButton(0)
  720.             
  721.             if read_only:
  722.                 onRadioButton.setEnabled(False)
  723.                 offRadioButton.setEnabled(False)
  724.                 defaultPushButton.setEnabled(False)
  725.             else:
  726.                 QToolTip.add(defaultPushButton, self.__tr('Set to default value of "%1".').arg(x))
  727.             
  728.             defaultPushButton.setText("Default")
  729.  
  730.         elif typ == cups.PPD_UI_PICKONE: # Combo box widget
  731.             widget = self.getWidget()
  732.  
  733.             layout1 = QHBoxLayout(widget,5,10,"layout1")
  734.  
  735.             textLabel1 = QLabel(widget,"textLabel1")
  736.             layout1.addWidget(textLabel1)
  737.  
  738.             spacer1 = QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
  739.             layout1.addItem(spacer1)
  740.  
  741.             optionComboBox = OptionComboBox(0, widget, "optionComboBox", group, option, choices, default)
  742.             layout1.addWidget(optionComboBox)
  743.  
  744.             defaultPushButton = DefaultPushButton(widget,"defaultPushButton", group, option, 
  745.                 choices, default, optionComboBox, typ)
  746.  
  747.             optionComboBox.setDefaultPushbutton(defaultPushButton)
  748.  
  749.             layout1.addWidget(defaultPushButton)
  750.  
  751.             textLabel1.setText(text)
  752.             defaultPushButton.setText("Default")
  753.  
  754.             x, y = None, None
  755.             for c, t in choices:
  756.                 d = c.lower()
  757.                 if value is not None and d == value.lower():
  758.                     x = t
  759.  
  760.                 if d == default.lower():
  761.                     y = t
  762.  
  763.                 optionComboBox.insertItem(t)
  764.  
  765.             if x is not None:
  766.                 optionComboBox.setCurrentText(x)
  767.  
  768.             if value is not None and value.lower() == default.lower():
  769.                 defaultPushButton.setEnabled(False)
  770.                 
  771.             self.linkPrintoutModeAndQuality(option, value)
  772.  
  773.             if read_only:
  774.                 optionComboBox.setEnabled(False)
  775.                 defaultPushButton.setEnabled(False)
  776.             elif y is not None:
  777.                 QToolTip.add(defaultPushButton, self.__tr('Set to default value of "%1".').arg(y))
  778.  
  779.             self.connect(defaultPushButton, SIGNAL("clicked()"), self.defaultPushButton_clicked)
  780.             self.connect(optionComboBox, SIGNAL("activated(const QString&)"), self.optionComboBox_activated)
  781.             
  782.             control = optionComboBox
  783.  
  784.         elif typ == cups.UI_SPINNER: # Spinner widget
  785.             widget = self.getWidget()
  786.  
  787.             layout1 = QHBoxLayout(widget,5,10, "layout1")
  788.  
  789.             textLabel1 = QLabel(widget, "textLabel1")
  790.             layout1.addWidget(textLabel1)
  791.  
  792.             spacer1 = QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
  793.             layout1.addItem(spacer1)
  794.  
  795.             optionSpinBox = OptionSpinBox(widget,"optionSpinBox", group, option, default)
  796.             layout1.addWidget(optionSpinBox)
  797.  
  798.             defaultPushButton = DefaultPushButton(widget, "defaultPushButton", group, option, choices, 
  799.                 default, optionSpinBox, typ)
  800.  
  801.             optionSpinBox.setDefaultPushbutton(defaultPushButton)
  802.  
  803.             layout1.addWidget(defaultPushButton)
  804.  
  805.             min, max = choices
  806.             optionSpinBox.setMinValue(min)
  807.             optionSpinBox.setMaxValue(max)
  808.             optionSpinBox.setValue(value)
  809.  
  810.             if suffix:
  811.                 optionSpinBox.setSuffix(suffix)
  812.  
  813.             textLabel1.setText(text)
  814.             defaultPushButton.setText("Default")
  815.  
  816.             self.connect(optionSpinBox, SIGNAL("valueChanged(int)"), self.optionSpinBox_valueChanged)
  817.             self.connect(defaultPushButton, SIGNAL("clicked()"), self.defaultPushButton_clicked)
  818.  
  819.             if value == default:
  820.                 defaultPushButton.setEnabled(False)
  821.  
  822.             if read_only:
  823.                 self.optionSpinBox.setEnabled(False)
  824.                 self.defaultPushButton.setEnabled()
  825.             else:
  826.                 QToolTip.add(defaultPushButton, 
  827.                     self.__tr('Set to default value of "%1".').arg(default))
  828.  
  829.         elif typ == cups.UI_BANNER_JOB_SHEETS:  # Job sheets widget
  830.             widget = self.getWidget()
  831.             
  832.             layout1 = QGridLayout(widget,1,1,5,10,"layout1")
  833.     
  834.             startComboBox = OptionComboBox(0, widget, "startComboBox", group, 
  835.                 "start", choices, default, typ)
  836.                 
  837.             layout1.addWidget(startComboBox,0,3)
  838.     
  839.             startTextLabel = QLabel(widget,"startTextLabel")
  840.             layout1.addWidget(startTextLabel,0,2)
  841.     
  842.             endTextLabel = QLabel(widget,"endTextLabel")
  843.             layout1.addWidget(endTextLabel,0,4)
  844.     
  845.             endComboBox = OptionComboBox(0, widget, "endComboBox", group, "end", choices, 
  846.                 default, typ, startComboBox)
  847.                 
  848.             layout1.addWidget(endComboBox,0,5)
  849.             
  850.             startComboBox.setOther(endComboBox)
  851.     
  852.             defaultPushButton = DefaultPushButton(widget, "defaultPushButton", group, option, choices, 
  853.                 default, (startComboBox, endComboBox), typ)
  854.             
  855.             layout1.addWidget(defaultPushButton,0,6)
  856.  
  857.             startComboBox.setDefaultPushbutton(defaultPushButton)
  858.             endComboBox.setDefaultPushbutton(defaultPushButton)
  859.             
  860.             textLabel1 = QLabel(widget,"textLabel1")
  861.             layout1.addWidget(textLabel1,0,0)
  862.             
  863.             spacer1 = QSpacerItem(20,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
  864.             layout1.addItem(spacer1,0,1)
  865.             
  866.             textLabel1.setText(text)
  867.             defaultPushButton.setText("Default")
  868.             
  869.             startTextLabel.setText(self.__tr("Start:"))
  870.             endTextLabel.setText(self.__tr("End:"))
  871.  
  872.             s, e, y, z = None, None, None, None
  873.             for c, t in choices:
  874.                 d = c.lower()
  875.                 if value is not None:
  876.                     if d == value[0].lower():
  877.                         s = t
  878.                     
  879.                     if d == value[1].lower():
  880.                         e = t
  881.  
  882.                 if d == default[0].lower():
  883.                     y = t
  884.                     
  885.                 if d == default[1].lower():
  886.                     z = t
  887.  
  888.                 startComboBox.insertItem(t)
  889.                 endComboBox.insertItem(t)
  890.  
  891.             if s is not None:
  892.                 startComboBox.setCurrentText(s)
  893.                 
  894.             if e is not None:
  895.                 endComboBox.setCurrentText(e)
  896.  
  897.             if value is not None and \
  898.                 value[0].lower() == default[0].lower() and \
  899.                 value[1].lower() == default[1].lower():
  900.                 
  901.                 defaultPushButton.setEnabled(False)
  902.  
  903.             if y is not None and z is not None:
  904.                 QToolTip.add(defaultPushButton, self.__tr('Set to default value of "Start: %1, End: %2".').arg(y).arg(z))
  905.  
  906.             self.connect(startComboBox, SIGNAL("activated(const QString&)"), self.optionComboBox_activated)
  907.             self.connect(endComboBox, SIGNAL("activated(const QString&)"), self.optionComboBox_activated)
  908.             self.connect(defaultPushButton, SIGNAL("clicked()"), self.defaultPushButton_clicked)
  909.             
  910.         elif typ == cups.PPD_UI_PICKMANY:
  911.             print "pickmany"
  912.  
  913.         elif typ == cups.UI_UNITS_SPINNER:
  914.             widget = self.getWidget()
  915.             
  916.             layout1 = QHBoxLayout(widget,5,10,"layout1")
  917.  
  918.             textLabel1 = QLabel(widget,"textLabel1")
  919.             layout1.addWidget(textLabel1)
  920.             
  921.             spacer1 = QSpacerItem(20,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
  922.             layout1.addItem(spacer1)
  923.     
  924.             lineEdit1 = QLineEdit(widget,"lineEdit1")
  925.             layout1.addWidget(lineEdit1)
  926.     
  927.             comboBox1 = QComboBox(0,widget,"comboBox1")
  928.             layout1.addWidget(comboBox1)
  929.     
  930.             defaultPushButton = QPushButton(widget,"defaultPushButton")
  931.             layout1.addWidget(defaultPushButton)
  932.             
  933.             textLabel1.setText(text)
  934.             defaultPushButton.setText("Default")
  935.             
  936.         else:
  937.             log.error("Invalid UI value: %s/%s" % (group, option))
  938.  
  939.         if widget is not None:
  940.             self.addWidget(widget, "o:"+option, control)
  941.             return widget
  942.             
  943.  
  944.  
  945.     def __tr(self,s,c = None):
  946.         return qApp.translate("DevMgr4",s,c)
  947.  
  948.